<?php
namespace Tyrone\Command;
use Medoo\Medoo;
use Tyrone\Model\Game\LogLogin;
use Swoole\Coroutine;
use Tyrone\Model\Game\PayLog;
use Tyrone\Model\PlayerPay;

class PlayerOnline extends Base
{



    public  function execute()
    {
        $datas = [];
        $database = $this->database;
        $game_database = $this->game_mysql;
        $mysql = $this->mysql;
        $this->setFrequency(300);
        $day = date("Ymd",strtotime($this->sDate));
        $eDate = date("Ymd",strtotime($this->eDate));

        $logLoginModel = new LogLogin($game_database);
        $PlayerPayModel = new PlayerPay($mysql);
        $playerOnlineModel = new \Tyrone\Model\PlayerOnline($mysql);

        while($day <= $eDate) {
            //统计登录数据
            $start_time = strtotime($day . ' 00:00:00');
            $end_time = strtotime($day . ' 23:59:59');
            $data = $logLoginModel->getLoginInfo($day,$this->database);
            if($data){
                //获取当天充值的玩家
                $payPlayerArr = $PlayerPayModel->getRoleGroupData($day);

                //获取历史充值玩家(过滤到getLoginInfo登录记录里的角色
                $historyPayPlayerArr = $game_database->select('role_pay_log(pg)',[
                    '[><]log_login(lg)' => ['pg.role_id' => 'role_id']
                ],[
                    'pg.role_id(player_id)',
                    'rmb' =>Medoo::raw('SUM(pg.rmb)'),
                ],[
                    'lg.time[<>]' => [$start_time, $end_time],
                ]);
                //前一天
                $beforeDay = date("Ymd", strtotime($day) - 86400);

                //查询昨日玩家的连续登录天数，充值天数
                $beforeData = $playerOnlineModel->getDayPlayerData($beforeDay);
            }

            foreach($data as $vo){
                $save = $vo;
                $save["day"] = $day;
                $save['plat_server'] = $this->plat_server;
                $save['pay_rmb'] = $vo["pay_rmb"] ?: 0;
                $save["month"] = date("Ym", strtotime($vo["day"]));
                //保证字段一致
                $save["is_new"] = 0;
                $save["is_pay"] = 0;
                $save["contin_pay"] = 0;
                //当天注册
                if(date("Ymd", $vo["reg_time"]) == $day){
                    $save["is_new"] = 1;
                }
                unset($save["reg_time"]);
                //历史充值
                if(isset($historyPayPlayerArr[$vo["player_id"]])){
                    $save["is_pay"] = 2;
                    $save["contin_pay"] = isset($beforeData[$vo["player_id"]]) ? $beforeData[$vo["player_id"]]["contin_pay"] + 1 : 1;
                }

                //当天充值
                if(isset($payPlayerArr[$vo["player_id"]])){
                    $save["is_pay"] = 1;
                    $save["contin_pay"] = isset($beforeData[$vo["player_id"]]) ? $beforeData[$vo["player_id"]]["contin_pay"] + 1 : 1;
                }
                $datas[] = $save;
            }

            $day = date('Ymd',strtotime($day) + 86400);
        }
        foreach (array_chunk($datas,1000) as $items){
            $playerOnlineModel->updateData($items);
            usleep(100);
        }


        return $this->result([count($datas)]);
    }
}